package com.malphi.framework.flash;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.malphi.framework.flash.jsonparse.FlashRequestParser;
import com.malphi.framework.flash.jsonparse.InvocationMethod;
import com.malphi.framework.log.LogFactory;

/**
 * flash 请求处理类
 * 双方采用json加密串通信
 * @author mrf
 *
 */
public class FlashServlet extends HttpServlet {

	private static final long serialVersionUID = 5911676178164699339L;
	
	private static final String FLASH_DATA = "data";

	@Override
	protected void doGet(HttpServletRequest request,HttpServletResponse response){
		String data = request.getParameter(FLASH_DATA);
		if(StringUtils.isNotBlank(data)){
			List<InvocationMethod> methods = FlashRequestParser.getJsonData(data);
			try {
				for(InvocationMethod inv : methods){
					Method method = FlashClient.class.getMethod(inv.getName(), inv.getParamsClass());
					Object methodResult = method.invoke(null, inv.getParamValues());
				}
			} catch (Exception e) {
				LogFactory.print("invoke method error!");
				e.printStackTrace();
			}
			
		}else{
			try {
				response.getWriter().write("empty data");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
}
